'''
Created on Mar 7, 2016

@author: JimmyMo
'''
import sys
import traceback
import time

from reader.perfsynreader_redis import Reader4Redis
from reader.perfsynreader_generator import Reader4Generator

from writter.perfsynwritter_file import Writter4File
from writter.perfsynwritter_es import Writter4ES
from writter.perfsynwritter_mongodb import Writter4MongoDb
from writter.perfsynwritter_redis import Writter4Redis

from processor.perfsynprocessor_delta import DeltaProcessor
from processor.perfsynprocessor_drop import DropProcessor

# from processor import perfsynprocessor_delta.DeltaProcessor
def start():
    try:
        now = time.clock()
    #     reader = Reader4Generator()
        reader = Reader4Redis()
        delta = DeltaProcessor()
        drop = DropProcessor()
        es = Writter4ES()
        es.flush_db()
        file = Writter4File()
        file.flush_db()
        redis = Writter4Redis()
#         es = Writter4MongoDb()
        
        reader.set_successor(delta) \
                .set_successor(drop) \
                    .set_successor(es) \
                        .set_successor(file) \
                            .set_successor(redis) \
                                .set_successor(None)
        reader.read()
#         logs4all_devices = reader.read()
    #     es = Writter4ES(logs4all_devices)
    #     es.write()
    #     es = Writter4File(logs4all_devices)
    #     es.write()
        
#         es.write()
        elapsed = time.clock() - now
        print("overall took %fs" % elapsed)
    except Exception as e:
        print("Printing only the traceback above the current stack frame")
        print("".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
        print("Printing the full traceback as if we had not caught it here...")
        print(format_exception(e))

def format_exception(e):
    exception_list = traceback.format_stack()
    exception_list = exception_list[:-2]
    exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
    exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))

    exception_str = "Traceback (most recent call last):\n"
    exception_str += "".join(exception_list)
    # Removing the last \n
    exception_str = exception_str[:-1]

    return exception_str

if __name__ == "__main__":
    start()